package com.hurl.waguji.biz.download;

import java.util.ArrayList;
import java.util.List;

import org.testng.log4testng.Logger;

import com.hurl.library.spider.UrlPattern;
import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.Formatter;
import com.hurl.waguji.base.dataprocess.ProcessParams;
import com.hurl.waguji.persistence.DmGpBO;
import com.hurl.waguji.persistence.JcsjFhrzPgbHexunBO;
import com.hurl.waguji.persistence.JcsjFhrzPgbHexunBOId;

public class ProfilePgHexunProcessor extends BaseDownloadForUpdateProcessor {
	public ProfilePgHexunProcessor(ProcessParams params,
			DownloadIdentifier identifier) {
		super(params, identifier);
	}

	private static final Logger log = Logger.getLogger(ProfilePgHexunProcessor.class);
	
	@Override
	public UrlPattern getUrlPattern() {
		return new StockIteratorUrlPattern(context) {
			
			@Override
			public String bo2url(DmGpBO bo) {
				return "http://stockdata.stock.hexun.com/2009_pg_"+bo.getGpDmJ()+".shtml";
			}
		};
	}

	private List<JcsjFhrzPgbHexunBO> html2BO(String gpdm, String html) {
		if(StringUtils.isBlank(html)){
			return null;
		}
		List<String> tablelist = StringUtils.substringsBetween(html, "<table", "</table>");
		if(tablelist==null || tablelist.isEmpty()){
			return null;
		}
		List<JcsjFhrzPgbHexunBO> result = new ArrayList<JcsjFhrzPgbHexunBO>();
		JcsjFhrzPgbHexunBO bo = null;
		for(String table : tablelist){
			if(!table.contains("公告日期")){
				continue;
			}
			bo = new JcsjFhrzPgbHexunBO();
			
			String value = StringUtils.substringBetween(table, "<strong>公告日期</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setId(new JcsjFhrzPgbHexunBOId(gpdm,Formatter.toDate(value)));
			
			value = StringUtils.substringBetween(table, "配股比例(每股)</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgbl(Formatter.toDouble(value));

			value = StringUtils.substringBetween(table, "配股价格</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgjg(Formatter.toDouble(value));

			value = StringUtils.substringBetween(table, "<strong>配股缴款开始日</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgjkksr(Formatter.toDate(value));

			value = StringUtils.substringBetween(table, "<strong>配股缴款截止日</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgjkjzr(Formatter.toDate(value));

			value = StringUtils.substringBetween(table, "<strong>配股代码</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgdm(Formatter.toString(value));

			value = StringUtils.substringBetween(table, "<strong>配股登记日</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgdjr(Formatter.toDate(value));

			value = StringUtils.substringBetween(table, "<strong>配股除权日</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgcqr(Formatter.toDate(value));

			value = StringUtils.substringBetween(table, "<strong>配股上市日</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgssr(Formatter.toDate(value));

			value = StringUtils.substringBetween(table, "<strong>计划募集资金</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setJhmjzj(Formatter.toDouble(value));

			value = StringUtils.substringBetween(table, "<strong>实际募集资金</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setSjmjzj(Formatter.toDouble(value));

			value = StringUtils.substringBetween(table, "<strong>配股承销商</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setPgcxs(Formatter.toString(value));

			value = StringUtils.substringBetween(table, "<strong>备注</strong></div></td>", "</td>");
			value = StringUtils.substringBetween(value, ">", null);
			bo.setBz(Formatter.toString(value));
			
			bo.setLastupdate(currentTime);
			result.add(bo);
		}
		return result;
	}
	
	@Override
	public void convertHTML2BO(String url, String html, BOSaver collector) {

		if(StringUtils.isBlank(html)){
			return;
		}

		String gpdm = context.getEnvAsString("gpdm");
		
		List<JcsjFhrzPgbHexunBO> list = html2BO(gpdm,html);
		 UniqueChecker checker = UniqueChecker.getInstance();
		if(list!=null && list.size()>0){
			for(JcsjFhrzPgbHexunBO bo : list){
				if(checker.isUnique(bo.getId().getGpdm()+bo.getId().getGgr()))
                    collector.addBO(bo);
				else
					log.error("数据重复");
			}
		}
	}

}
